import 'package:flutter/material.dart';
//import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_app/bean/Shop.dart';
import 'MainWidget1.dart';
import 'package:flutter_app/widget/AppBarTemp1.dart';
class ShoppingCart extends StatefulWidget {
  final callback;

  ShoppingCart(this.callback);
  @override
  State<ShoppingCart> createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State<ShoppingCart> with AutomaticKeepAliveClientMixin{
  bool check = false;
  List<Shop> list = List();
  int total = 0;
  int num=0;
  @override
  bool get wantKeepAlive => true;
  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 100; i++) {
      list.add(new Shop("商品 $i", false, 1,1));
    }

//    Fluttertoast.showToast(msg: "购物车");

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBarTemp1(
          child: Container(
            margin:
            EdgeInsets.only(left: 10.0, top: 10.0, bottom: 10.0, right: 10.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  "购物车",
                  style: TextStyle(
                      fontSize: 18.0,
                      color: Color(0xff121212),
                      fontWeight: FontWeight.bold),
                ),
                Container(
                  width: 103.0,
                  height: 35.0,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20.0),
                      border: Border.all(color: Color(0xffededed))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 50.0,
                        height: 40.0,
                        child: Image.asset("images/menu.png"),
                      ),
                      Container(
                        height: 30.0,
                        width: 1.0,
                        color: Color(0xffededed),
                      ),
                      Container(
                        width: 50.0,
                        height: 40.0,
                        padding: EdgeInsets.only(
                            top: 10.0, bottom: 10.0, left: 5.0, right: 5.0),
                        child: Image.asset(
                          "images/round.png",
                          width: 20.0,
                          height: 20.0,
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
        body: ListView.builder(
            itemCount: list.length,
            itemBuilder: (_, int id) {
              return new Row(
                children: <Widget>[
                  Checkbox(
                      value: list[id].check,
                      onChanged: (check) {
                        list[id].check = check;
                        total = 0;
                        num=0;
                        for (Shop shop in list) {
                          if (shop.check) {
                            total = total + shop.price*shop.num;
                            num=num +shop.num;
                          }
                        }
                        widget.callback(num);
                   //     Fluttertoast.showToast(msg: "price${list[id].price}num${list[id].num}");
                     //   firedA();
                        setState(() {});
                      }),
//                  Text("${list[id].name}   价格${list[id].price}")
                  shopItem(id)
                ],
              );
            }),
        bottomSheet: Container(
          height: 40.0,
          padding: EdgeInsets.only(left: 10.0, right: 10.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Checkbox(
                  value: check,
                  onChanged: (check) {
                    total = 0;
                    this.check = check;
                    for (Shop shop in list) {
                      shop.check = check;
                    }
                    for (Shop shop in list) {
                      if (shop.check) {
                        total = total + shop.price;
                      }
                    }
                    setState(() {});
                  }),
              Expanded(
                child: Text(""),
              ),
              Text("合计"),
              Text(
                "¥$total",
                style: TextStyle(color: Colors.red),
              ),
              Container(
                  padding: EdgeInsets.all(5.0),
                  decoration: BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.circular(10.0)),
                  child: Text(
                    "结算",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 19.0,
                    ),
                  )),
            ],
          ),
        ));
  }
  void firedA() {
    widget.callback(1);
  }
  Widget shopItem(index){
    return Container(
          margin: EdgeInsets.only(left: 10.0,right: 10.0),
          padding: EdgeInsets.only(top: 10.0,bottom: 10.0),
         child: Row(
           
           children: <Widget>[

             Image.asset("images/ex1.png",width: 60.0,height: 60.0,),
             SizedBox(width: 10.0,),
             Column(
               children: <Widget>[
                 Text("三养韩国进口火鸡泡面",style: TextStyle(color: Color(0xff333333)),),
                 Text("测试 | 测试",style: TextStyle(fontSize: 10.0,color: Color(0xffeeeeee)),),
                 Row(
                   children: <Widget>[
                     Text("¥${list[index].price}",style: TextStyle(color: Color(0xff333333))),
                     SizedBox(width: 10.0,),
                     Container(

                       height: 35.0,
                       decoration: BoxDecoration(
                           borderRadius: BorderRadius.circular(20.0),
                           border: Border.all(color: Color(0xffededed))),
                       child: Row(
                         children: <Widget>[
                           GestureDetector(
                           child:Container(
                             width: 50.0,
                             height: 40.0,
                             child: Image.asset("images/jian.png"),
                           ),
                             onTap: (){
                             if(list[index].num>0){
                               list[index].num--;
                               total=0;
                               num=0;
                               for (Shop shop in list) {
                                 if (shop.check) {
                                   total = total + shop.price*shop.num;
                                   num=num +shop.num;
                                 }
                               }
                               widget.callback(num);
                 //              Fluttertoast.showToast(msg: "price${list[index].price}num${list[index].num}");
                             }
                             setState(() {

                             });
                             },
                       )
                           ,
                           Container(
                             height: 30.0,
                             width: 1.0,
                             color: Color(0xffededed),
                           ),
                           SizedBox(width: 10.0,),
                           Text("${list[index].num}"),
                           SizedBox(width: 10.0,),
                           Container(
                             height: 30.0,
                             width: 1.0,
                             color: Color(0xffededed),
                           ),
                           GestureDetector(child: Container(
                             width: 50.0,
                             height: 40.0,
                             padding: EdgeInsets.only(
                                 top: 10.0, bottom: 10.0, left: 5.0, right: 5.0),
                             child: Image.asset(
                               "images/jia.png",
                               width: 20.0,
                               height: 20.0,
                             ),
                           ),
                           onTap: (){

                               list[index].num++;
                               num=0;
                               total=0;
                               for (Shop shop in list) {
                                 if (shop.check) {
                                   total = total + shop.price*shop.num;
                                   num=num +shop.num;
                                 }
                               }
                               widget.callback(num);
                             //  Fluttertoast.showToast(msg: "${list[index].num}");
                            //   Fluttertoast.showToast(msg: "price${list[index].price}num${list[index].num}");
                              setState(() {

                              });
                           },
                           )

                         ],
                       ),
                     )
                   ],

                 )
                 
               ],
             )

           ],

         ),
    );

  }
}
